package com.ruoyi.common.flowable.expression;

import org.flowable.variable.api.delegate.VariableScope;
import org.flowable.variable.api.persistence.entity.VariableInstance;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SimpleVariableScope implements VariableScope {
    private final Map<String, Object> variables = new HashMap<>();

    @Override
    public void setVariable(String variableName, Object value, boolean fetchAllVariables) {
        setVariable(variableName, value);
    }

    @Override
    public Object setVariableLocal(String variableName, Object value) {
        setVariable(variableName, value);
        return value;
    }

    @Override
    public Object setVariableLocal(String variableName, Object value, boolean fetchAllVariables) {
        return setVariableLocal(variableName, value);
    }

    @Override
    public void setVariables(Map<String, ?> variables) {
        this.variables.putAll(variables);
    }

    @Override
    public void setVariablesLocal(Map<String, ?> variables) {
        setVariables((Map<String, Object>) variables);
    }

    @Override
    public boolean hasVariables() {
        return !variables.isEmpty();
    }

    @Override
    public boolean hasVariablesLocal() {
        return hasVariables();
    }

    @Override
    public Map<String, Object> getVariables() {
        return new HashMap<>(variables);
    }

    @Override
    public Object getVariable(String name) {
        return variables.get(name);
    }

    @Override
    public Object getVariable(String variableName, boolean fetchAllVariables) {
        return getVariable(variableName);
    }

    @Override
    public Object getVariableLocal(String variableName) {
        return getVariable(variableName);
    }

    @Override
    public Object getVariableLocal(String variableName, boolean fetchAllVariables) {
        return getVariableLocal(variableName);
    }

    @Override
    public <T> T getVariable(String variableName, Class<T> variableClass) {
        Object value = getVariable(variableName);
        if (variableClass.isInstance(value)) {
            return variableClass.cast(value);
        }
        return null;
    }

    @Override
    public <T> T getVariableLocal(String variableName, Class<T> variableClass) {
        return getVariable(variableName, variableClass);
    }

    @Override
    public Set<String> getVariableNames() {
        return variables.keySet();
    }

    @Override
    public Set<String> getVariableNamesLocal() {
        return getVariableNames();
    }

    @Override
    public void setVariable(String variableName, Object value) {
       variables.put(variableName, value);
    }

    @Override
    public boolean hasVariable(String name) {
        return variables.containsKey(name);
    }

    @Override
    public boolean hasVariableLocal(String variableName) {
        return hasVariable(variableName);
    }

    @Override
    public void removeVariable(String variableName) {
        variables.remove(variableName);
    }

    @Override
    public void removeVariableLocal(String variableName) {
        removeVariable(variableName);
    }

    @Override
    public void removeVariables(Collection<String> variableNames) {
        for (String name : variableNames) {
            removeVariable(name);
        }
    }

    @Override
    public void removeVariablesLocal(Collection<String> variableNames) {
        removeVariables(variableNames);
    }

    @Override
    public void removeVariables() {
        variables.clear();
    }

    @Override
    public void removeVariablesLocal() {
        removeVariables();
    }

    // 以下方法是暂未支持的高级特性，保持空实现或返回 null 即可
    @Override public Map<String, VariableInstance> getVariableInstances() { return null; }
    @Override public Map<String, Object> getVariables(Collection<String> variableNames) { return null; }
    @Override public Map<String, VariableInstance> getVariableInstances(Collection<String> variableNames) { return null; }
    @Override public Map<String, Object> getVariables(Collection<String> variableNames, boolean fetchAllVariables) { return null; }
    @Override public Map<String, VariableInstance> getVariableInstances(Collection<String> variableNames, boolean fetchAllVariables) { return null; }
    @Override public Map<String, Object> getVariablesLocal() { return null; }
    @Override public Map<String, VariableInstance> getVariableInstancesLocal() { return null; }
    @Override public Map<String, Object> getVariablesLocal(Collection<String> variableNames) { return null; }
    @Override public Map<String, VariableInstance> getVariableInstancesLocal(Collection<String> variableNames) { return null; }
    @Override public Map<String, Object> getVariablesLocal(Collection<String> variableNames, boolean fetchAllVariables) { return null; }
    @Override public Map<String, VariableInstance> getVariableInstancesLocal(Collection<String> variableNames, boolean fetchAllVariables) { return null; }
    @Override public VariableInstance getVariableInstance(String variableName) { return null; }
    @Override public VariableInstance getVariableInstance(String variableName, boolean fetchAllVariables) { return null; }
    @Override public VariableInstance getVariableInstanceLocal(String variableName) { return null; }
    @Override public VariableInstance getVariableInstanceLocal(String variableName, boolean fetchAllVariables) { return null; }
    @Override public void setTransientVariable(String variableName, Object variableValue) {}
    @Override public void setTransientVariableLocal(String variableName, Object variableValue) {}
    @Override public void setTransientVariables(Map<String, Object> transientVariables) {}
    @Override public Object getTransientVariable(String variableName) { return null; }
    @Override public Map<String, Object> getTransientVariables() { return null; }
    @Override public void setTransientVariablesLocal(Map<String, Object> transientVariables) {}
    @Override public Object getTransientVariableLocal(String variableName) { return null; }
    @Override public Map<String, Object> getTransientVariablesLocal() { return null; }
    @Override public void removeTransientVariableLocal(String variableName) {}
    @Override public void removeTransientVariable(String variableName) {}
    @Override public void removeTransientVariables() {}
    @Override public void removeTransientVariablesLocal() {}
    @Override public String getTenantId() { return null; }
}
